package com.haiyu.manager.controller.platform;

import com.haiyu.manager.pojo.Message;
import com.haiyu.manager.response.PageDataResult;
import com.haiyu.manager.service.MessageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("message")//指定路由
public class MessageController {
    @Autowired
    MessageService messageService;
    private Logger logger = LoggerFactory.getLogger(this.getClass());



    /**
     *
     * 功能描述: 跳到列表
     *
     *
     */
    @RequestMapping("/messageManage")
    public String boardManage() {
        return "/message/messageManage";
    }

    /**
     *
     * 功能描述: 分页查询列表
     *
     *
     */
    @RequestMapping(value = "/getMessageList", method = RequestMethod.POST)
    @ResponseBody
    public PageDataResult getMessageList(@RequestParam("pageNum") Integer pageNum,
                                       @RequestParam("pageSize") Integer pageSize/*@Valid PageRequest page,*/ ) {
        /*logger.info("分页查询用户列表！搜索条件：userSearch：" + userSearch + ",pageNum:" + page.getPageNum()
                + ",每页记录数量pageSize:" + page.getPageSize());*/
        System.out.println();
        PageDataResult pdr = new PageDataResult();
        try {
            if(null == pageNum) {
                pageNum = 1;
            }
            if(null == pageSize) {
                pageSize = 10;
            }
            // 获取用户列表
            pdr = messageService.getMessageList( pageNum ,pageSize);
            logger.info(" message列表查询=pdr:" + pdr);

        } catch (Exception e) {
            e.printStackTrace();
            logger.error(" message列表查询异常！", e);
        }
        return pdr;
    }



    /**
     *
     * 功能描述: 更新board
     *
     *
     */
    @RequestMapping(value = "/setMessage", method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> setMessage(Message message) {
        logger.info("设置 message[更新]！ message:" +  message);
        System.out.println(" message");
        Map<String,Object> data = new HashMap();
        if(message.getMessageId() != null) {
            data = messageService.updateMessage(message);
        }else {
            data=messageService.addMessage(message);
        }
        return data;
    }


    /**
     *
     * 功能描述: 删除/恢复
     *
     *
     */
    @RequestMapping(value = "/updateMessageStatus", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> updateMessageStatus(@RequestParam("messageId") Integer messageId,@RequestParam("textStatus") Integer textStatus) {
        logger.info("删除/恢复！messageId:" + messageId+" textStatus:"+textStatus);
       System.out.println(" textStatus:"+textStatus);
        Map<String, Object> data = new HashMap<>();
        if(textStatus == 1){
            //删除
            data =messageService.delMessage(messageId,textStatus);
        }else{
            //恢复liuyan
            data = messageService.recoverMessage(messageId,textStatus);
        }
        return data;
    }
}
